package com.nz2004.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nz2004.entity.AdminUser;
import com.nz2004.entity.MyMsg;
import com.nz2004.mapper.AdminUserMapper;
import com.nz2004.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("adminUserService")
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public AdminUser queryAdminUserByEmail(String email, String password) {
        AdminUser adminUser = adminUserMapper.queryAdminUserByEmail(email);
        System.out.println(adminUser);

        if (adminUser != null && password.equals(adminUser.getPassword())) {
            //登录成功
            return adminUser;
        }
        //登录失败
        return null;

    }

    @Override
    @Transactional
    public Boolean insertAdminUser(AdminUser adminUser) {

        int i = adminUserMapper.insertAdminUser(adminUser);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public MyMsg queryUsers(int pageNum, int pageSize, String email, Integer status) {

        if(email == ""){
            email = null;
        }
        PageHelper.startPage(pageNum, pageSize);
        Page page = (Page<AdminUser>) adminUserMapper.queryUsers(email, status);
//        List<AdminUser> adminUsers = adminUserMapper.queryUsers(email, status);
        MyMsg myMsg = new MyMsg();
        myMsg.setMsg("没有查询到呢2");
        myMsg.setData(page);
        myMsg.setCount(page.getTotal());
        return myMsg;
    }

    @Override
    public boolean deleteAdminUserById(int id) {
        int i = adminUserMapper.deleteAdminUserById(id);
        if (i>0){
            //删除成功
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean updateAdminUser(AdminUser adminUser) {
        int i = adminUserMapper.updateAdminUser(adminUser);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
